package com.whfc.emp.xxl.job.generateData;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.whfc.XxlJobConfig;
import com.whfc.common.util.DateUtil;
import com.whfc.emp.param.EmpAttendSyncDataParam;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author ：pyl
 * @date ：Created in 2022/6/16 10:28
 * @description：人员管理模块数据初始化任务
 */
@Component
@ConditionalOnBean(XxlJobConfig.class)
public class EmpInitDataJob {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @XxlJob("EmpInitData")
    public void generateEmpData() {
        XxlJobHelper.log("开始生成人员数据...");
        logger.info("开始生成人员数据...");
        String paramStr = XxlJobHelper.getJobParam();
        JSONObject param = null;
        if (StringUtils.isEmpty(paramStr)) {
            XxlJobHelper.handleFail("没有配置参数");
            return;
        }

        param = JSONObject.parseObject(paramStr);
        JSONObject jsonObject = param.getJSONObject("data");
        String url = param.getString("url");
        List<Date> dates = new ArrayList<>();
        Date dateNow = new Date();
        if (param.containsKey("time")) {
            Date date = param.getDate("time");
            Date startDate = DateUtil.getDateBegin(date);
            Date enhdDate = DateUtil.getDateEnd(date);
            dates = DateUtil.getDayListOfMinute(startDate, enhdDate, 10);
        } else {
            dates.add(dateNow);
        }
//        String data = "{\"personGuid\":\"88F51C307647420AAD2370BBB4E16126\",\"deviceKey\":\"84E0F42668581502\",\"photoUrl\":\"https://uniubi-aiot.oss-cn-hangzhou.aliyuncs.com/device/84E0F42668581502/20220817164305_955_rgb.jpg\",\"showTime\":\"2022-08-17T08:43:05+0000\",\"data\":null,\"type\":1,\"temperature\":null,\"idCardInfo\":\"\"}";
//         = JSONObject.parseObject(data);
        EmpAttendSyncDataParam attendSyncDataParam = JSONObject.parseObject(jsonObject.toJSONString(),EmpAttendSyncDataParam.class);
        XxlJobHelper.log(JSONObject.toJSONString(attendSyncDataParam));
        attendSyncDataParam.setShowTime(new Date());
        String post = HttpUtil.post(url.concat("personGuid=").concat(attendSyncDataParam.getPersonGuid()).concat("&")
                        .concat("deviceKey=").concat(attendSyncDataParam.getDeviceKey()).concat("&")
                        .concat("photoUrl=").concat(jsonObject.getString("photoUrl"))
//                        .concat("showTime=").concat(attendSyncDataParam.getShowTime().toString()).concat("&")
//                        .concat("data=").concat("").concat("&")
//                        .concat("type=").concat(attendSyncDataParam.getPersonGuid()).concat("&")
//                        .concat("temperature=").concat(attendSyncDataParam.getPersonGuid()).concat("&")
//                        .concat("idCardInfo=").concat(attendSyncDataParam.getIdCardNo())
                       , JSONObject.toJSONString(attendSyncDataParam));
//        logger.info("返回结果： "+post);
        XxlJobHelper.log("返回结果..."+post);
    }

}
